{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>大纲<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#功能\" data-toc-modified-id=\"功能-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>功能</a></span></li><li><span><a href=\"#介绍\" data-toc-modified-id=\"介绍-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>介绍</a></span></li><li><span><a href=\"#应用场景\" data-toc-modified-id=\"应用场景-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>应用场景</a></span></li><li><span><a href=\"#如何使用\" data-toc-modified-id=\"如何使用-4\"><span class=\"toc-item-num\">4&nbsp;&nbsp;</span>如何使用</a></span><ul class=\"toc-item\"><li><span><a href=\"#&quot;get&quot;\" data-toc-modified-id=\"&quot;get&quot;-4.1\"><span class=\"toc-item-num\">4.1&nbsp;&nbsp;</span>\"get\"</a></span></li><li><span><a href=\"#&quot;set&quot;\" data-toc-modified-id=\"&quot;set&quot;-4.2\"><span class=\"toc-item-num\">4.2&nbsp;&nbsp;</span>\"set\"</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 功能\n",
    "把一个方法变成属性调用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "是Python内置的装饰器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 应用场景"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Student():\n",
    "    def __init__(self):\n",
    "        self.score = 100\n",
    "\n",
    "s = Student()        \n",
    "s.score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.score =1000\n",
    "s.score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考上面的例子可以看出，通过实例s能够修改其属性的值，逻辑上score只能是数字，但是\n",
    "1. 如果自己不小心给其赋值了一个字符串\n",
    "1. 别人使用这个类的实例是赋值了其他类型的数据\n",
    "\n",
    "就会导致程序错误。如何在对score属性赋值的时候进行参数检查，只能赋值为int类型呢？方法如下：\n",
    "\n",
    "1. 定义Get、Set(这个实现很容易，但是不简洁，不说多，不详细介绍)\n",
    "1. 使用property装饰器"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 如何使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## \"get\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Student():\n",
    "    def __init__(self):\n",
    "        self._score = 100\n",
    "    \n",
    "    # 在方法上一行同一缩进使用@property\n",
    "    @property\n",
    "    def score(self):\n",
    "        return self._score\n",
    "    \n",
    "s = Student()        \n",
    "s.score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意这个时候，原来的属性score需要与方法socre区分开（即不能同名），否则Python解释器无法理解该返回属性呢，还是函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## \"set\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "99"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Student():\n",
    "    def __init__(self):\n",
    "        self._score = 100    \n",
    "    # 在方法上一行同一缩进使用@property\n",
    "    @property\n",
    "    def score(self):\n",
    "        return self._score    \n",
    "    \n",
    "    @score.setter\n",
    "    def score(self,value):\n",
    "        if not isinstance(value,int):\n",
    "            raise ValueError('score must be an int')\n",
    "        self._score = value\n",
    "        \n",
    "s = Student() \n",
    "s.score = 99\n",
    "s.score"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "大纲",
   "title_sidebar": "目录",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
